layui.use(['layer', 'laypage', 'element', 'form'], function () {
    //由于layer弹层依赖jQuery，所以可以直接得到
    var $ = layui.$,
        layer = layui.layer;

    var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块

    //表单
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });

    //执行一个laypage实例
    var laypage = layui.laypage;
    laypage.render({
        elem: 'blog-list' //注意，这里的 test1 是 ID，不用加 # 号
        ,
        count: $("#count").val() //数据总数，从服务端得到
        ,
        limit: 7,
        jump: function (obj, first) {
            if (!first) {
                $(".userex-contents ul").empty();
                change(obj.curr, obj.limit);
            }
        }
    });

    //分页
    function change(start, limit) {
        var id = $("#userid").val();
        $.get("/Blog/getblogdata/" + id,
            {
                start: start,
                limit: limit,
                id: id
            },
            function (data) {
                //加载后台返回的List集合数据
                console.log(data);
                // $(".main-list ul").append();
                for (var o in data) {
                    $(".userex-contents ul").append(
                        "<li><div class='userex-item'>\n" +
                        "<a href='/Blog/blogdetail/" + data[o]["bid"] + "'>" + data[o]["title"] + "</a>" +
                        "<div class='userex-con' text=" + data[o]["content"] + ">暂无内容</div>" +
                        "<div class='status'>" +
                        "<span>" +
                        "<b class='collection-line'>" + data[o]["date"] + "</b>" +
                        "</span>" +
                        "<em class='lline'></em>" +
                        "<span>阅读数</span>" +
                        "<span class='userex-num' text=" + data[o]["browseNumber"] + "></span>" +
                        "<em class='lline'></em>" +
                        "<span>收藏数</span>" +
                        "<span class='userex-num' text=" + data[o]["collectionNumber"] + "></span>" +
                        "</div>" +
                        "<div class='operation'>" +
                        "<span style='color: red' onclick='del(this);' value=" + data[o]["bid"] + " >删除</span>" +
                        "</div></div></li>"
                    );

                }
            },
            "json"
        );
    }

    spancolor();

    function spancolor() {
        $(".operation").find("span").css("color", "#3399EA");
        var i = 0;
        $(".operation").find("span").each(function () {
            // i++;
            // if (i % 3 == 0) {
            // 	$(".operation").find("span").eq(i - 1).css("color", "red");
            // }
            $(".operation").find("span").css("color", "red");
        });
    }

    del = function (obj) {
        var bid = $(obj).attr("value");
        $.ajax({
            url: "/Blog/blogdel",
            data: {"bid": bid},
            success: function (data) {
                layer.msg(data.msg);
                $(obj).parent().parent().parent("li").remove();
                show();
            }
        });
    };
    // 先初始化一次
    show();
    function show() {
        if($("#count").val()<=7){
            $("#blog-list").hide();
        }
        else {
            $("#blog-list").show();
        }
        if ($(".userex-contents ul").find("li").length <= 0) {
            $(".nothing").show();
        } else {
            $(".nothing").hide();
        }
    }

});
